<?php
namespace app\common;

use app\common\enum\ErrorCodeEnum;
use app\common\enum\MessageEnum;
use app\common\enum\msgCodeEnum;

class BaseResult {

    private static $res = array(
        "error" => ErrorCodeEnum::FAIL,
        "message" => MessageEnum::FAIL,
        "msgCode" => msgCodeEnum::FAIL,
        "content" => null
    );

    /**
     * 返回错误信息
     * @param  $message
     * @param  $error_code
     * @return array
     */
    public static function error($message = MessageEnum::FAIL, int $error_code = ErrorCodeEnum::FAIL, $content = null)
    {
        self::$res['error'] = $error_code;
        self::$res['message'] = $message;
        self::$res['content'] = $content;
        return self::$res;
    }

    /**
     * 获取正常信息
     * @param string $content
     * @param string $message
     * @return array
     */
    public static function succ($content = '', $message = MessageEnum::SUCCESS){
        self::$res['error'] = ErrorCodeEnum::SUCCESS;
        self::$res['message'] = $message;
        self::$res['content'] = $content;
        return self::$res;
    }

    /**
     * 通用返回信息
     * @param array $params
     * @return array
     */
    public static function go(array $params = [])
    {
        if(array_key_exists('error', $params)) self::$res['error'] = $params['error'];
        if(array_key_exists('message', $params)) self::$res['message'] = $params['message'];
        if(array_key_exists('content', $params)) {
            self::$res['content'] = $params['content'];
        }else{
            self::$res['content'] = null;
        }
        if(array_key_exists('msgCode', $params)) self::$res['msgCode'] = $params['msgCode'];

        return self::$res;
    }
}
